package com.woniuxy.woniuhis.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.woniuxy.woniuhis.entity.Emp;
import com.woniuxy.woniuhis.service.EmpService;

public class UserRealm extends AuthorizingRealm{
	
	@Autowired 
	EmpService empService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("----------------------doGetAuthenticationInfo------------------");	
		// 获取身份信息
		Object principal = token.getPrincipal();
		String credentials = new String((char[])token.getCredentials());
		System.out.println("AuthenticationInfo principal:"+principal);
		System.out.println("AuthenticationInfo credentials:"+credentials);
		//从数据库中获取用户，包括用户名和密码
		Emp emp = empService.findEmpByEmpName(principal.toString());
		if(emp == null) {
			return null;
		}
		if(!principal.equals(emp.getEmpName())) {
			return null;
		}
		SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(emp, emp.getEmpPassword(), this.getName());
		return simpleAuthenticationInfo;
	}

}
